1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8
9 package org.webdocwf.util.smime.cms;
10
11
12 import org.webdocwf.util.smime.exception.SMIMEException;
13 import org.webdocwf.util.smime.exception.ErrorStorage;
14 import org.webdocwf.util.smime.der.DERSequencePr;
15 import org.webdocwf.util.smime.der.DERObjectIdentifier;
16 import org.webdocwf.util.smime.util.DERLengthSearcher;
17 import org.webdocwf.util.smime.util.ByteArrayComparator;
18 import java.security.cert.X509Certificate;
19
20
21 /***
22 * IssuerName class is DER encoded object represented in ASN.1 notation
23 * according to RFC2630. It is used for representing information about issuer
24 * of particular certificates. Detail information about ASN.1 notation of
25 * this class can be found in description of ASN.1 notation of IssuerAndSerialNumber.
26 */
27 public class IssuerName extends DERSequencePr {
28
29 /***
30 * Container for DN (set of distinguished names)
31 */
32 private byte[] dNames;
33
34 /***
35 * Enables/Disables function for particular adding of Relative Distinguished Name
36 */
37 private int enable = 0;
38
39 /***
40 * Construction with information got from specific X509Certificate or from .cer
41 * file information which is extracted into instance of X509Certificate class
42 * @param cert0 X509Certificate
43 * @exception SMIMEException caused by non SMIMEException which is:
44 * CertificateEncodingException. Also, it can be thrown by super class
45 * constructor.
46 */
47 public IssuerName(X509Certificate cert0) throws SMIMEException {
48 byte[] tbs = null;
49
50 try {
51 tbs = cert0.getTBSCertificate();
52 } catch (Exception e) {
53 throw SMIMEException.getInstance(this, e, "constructor");
54 }
55 dNames = findDNfromTBS(tbs);
56 }
57
58 /***
59 * Finds area with Distinguish Names from TBS Certificate part of X509
60 * certificate, represented as byte array
61 * @param tbs0 TBS Certificate represented as byte array
62 * @return Distinguish name as byte array
63 */
64 private byte[] findDNfromTBS(byte[] tbs0) {
65 int start = 0; // first SEQUENCE tag in TBSCertificate
66 byte[] temp;
67 DERLengthSearcher len = new DERLengthSearcher(start, tbs0);
68
69 start = start + len.getLengthtDERLengthPart() + 1; // [0]
70 len.newInitialization(start, tbs0);
71 start = start + len.getLengthtDERLengthPart() + len.getLengthtDERContentPart() + 1; // CertificateSerialNumber
72 len.newInitialization(start, tbs0);
73 start = start + len.getLengthtDERLengthPart() + len.getLengthtDERContentPart() + 1; // Algorythm identifier - SEQUENCE
74 len.newInitialization(start, tbs0);
75 start = start + len.getLengthtDERLengthPart() + len.getLengthtDERContentPart() + 1; // Issuer Name - SEQUENCE
76 len.newInitialization(start, tbs0);
77 start = start + len.getLengthtDERLengthPart() + 1;
78 int stop = start + len.getLengthtDERContentPart() - 1;
79
80 temp = new byte[stop - start + 1];
81 for (int i = start; i <= stop; i++)
82 temp[i - start] = tbs0[i];
83 return temp;
84 }
85
86 /***
87 * Adds all Relative Distinguish Names from certificate to IssuerName
88 * @exception SMIMEException thrown from super class addContent method.
89 */
90 public void addAllRelativeDN() throws SMIMEException {
91 super.addContent(dNames);
92 enable = 1;
93 }
94
95 /***
96 * Adds particular Relative Distinguish Name from certificate to IssuerName.
97 * This method can be called many times, but never if method
98 * addAllRelativeDN was called first
99 * @param id_at_type0 object identifier name of desired Particular Distinguish
100 * Name
101 * @return Desired Particular Distinguish Name as byte array
102 * @exception SMIMEException if method addAllRelativeDN was already performed.
103 * Also it can be caused by non SMIMEException which is:
104 * UnsupportedEncodingException.
105 */
106 public int addParticularRelativeDN(String id_at_type0) throws SMIMEException {
107 if (enable == 1)
108 throw new SMIMEException(this, 1021);
109 byte[] temp = new DERObjectIdentifier(id_at_type0, "NAME_STRING").getDEREncoded();
110 ByteArrayComparator bcomp = new ByteArrayComparator(temp, dNames);
111 int positionFirst = bcomp.getMatchingIndex();
112
113 if (positionFirst != -1) // Matching is founded
114 {
115 positionFirst = positionFirst + temp.length;
116 DERLengthSearcher len = new DERLengthSearcher(positionFirst, dNames);
117
118 positionFirst = positionFirst + len.getLengthtDERLengthPart() + 1;
119 int positionLast = positionFirst + len.getLengthtDERContentPart() - 1;
120 byte[] name = new byte[positionLast - positionFirst + 1];
121
122 for (int i = positionFirst; i <= positionLast; i++) // Finding a text of particular distinguish name
123 name[i - positionFirst] = dNames[i];
124 RelativeDistinguishedName rdn = null;
125
126 try {
127 rdn = new RelativeDistinguishedName(id_at_type0, "NAME_STRING", new String(name, "ISO-8859-1"));
128 } catch (Exception e) {
129 throw SMIMEException.getInstance(this, e, "addParticularRelativeDN");
130 }
131 super.addContent(rdn.getDEREncoded());
132 return 0; // success of operation
133 } else
134 return -1; // failure of operation
135 }
136 }
137
This page was automatically generated by Maven